
 /**
 * HibernateGroupDao.java.java.
 * User: BrianChen Date: 2012-6-7 下午09:35:27
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.security.dao.impl;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.collapsar.core.orm.hibernate.BaseHibernateDao;
import com.collapsar.core.security.dao.IGroupDao;
import com.collapsar.core.security.model.Group;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-6-7 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-6-7 下午09:35:27
 * @version 1.0
 * @see
 * 
 */
public class HibernateGroupDao extends BaseHibernateDao implements IGroupDao{

	@Override
	public void storeGroup(Group group) {
		this.store(group); 
	}

	@Override
	public void deleteGroup(Long groupId) {
		this.delete(this.load(Group.class, groupId));
	}

	@Override
	public void deleteAllGroups(Long[] groupIds) {
		this.deleteAll(Group.class, groupIds);
	}

	@Override
	public Group loadGroup(Long groupId) {
		return (Group)this.load(Group.class, groupId);
	}

//	@Override
//	public Group loadGroupWithRoles(Long groupId) {
//		Group group = (Group)this.load(Group.class, groupId);
//		this.initalize(group.getRoles());
//		return group;
//	}

	@Override
	public Group loadGroupWithUsers(Long groupId) {
		Group group = (Group)this.load(Group.class, groupId);
		this.initalize(group.getUsers());
		return group;
	}

	@Override
	public Group loadGroupWithRolesAndUsers(Long groupId) {
		Group group = (Group)this.load(Group.class, groupId);
		//this.initalize(group.getRoles());
		this.initalize(group.getUsers());
		return group;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Group getGroupByName(final String groupName) {
		Group group = (Group)this.getHibernateTemplate().execute(
				new HibernateCallback(){
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.getNamedQuery("GetGroupByName")
							.setParameter("groupName", groupName)
							.uniqueResult();
					}}
				);
		//this.initalize(group.getRoles());
		this.initalize(group.getUsers());
		return group;
	}

}

